//
//  HomeTableViewCell.swift
//  SwiftWB
//
//  Created by 钟清 on 17/4/1.
//  Copyright © 2017年 Zhong Qing. All rights reserved.
//

import UIKit
import SDWebImage
import KILabel

class HomeTableViewCell: UITableViewCell {

    /* 头像 */
    @IBOutlet weak var iconImageView: UIImageView!
    /* 认证图标 */
    @IBOutlet weak var verifiedImageView: UIImageView!
    /* 昵称 */
    @IBOutlet weak var nameLabel: UILabel!
    /* 会员图标 */
    @IBOutlet weak var vipImageView: UIImageView!
    /* 时间 */
    @IBOutlet weak var timeLabel: UILabel!
    /* 来源 */
    @IBOutlet weak var sourceLabel: UILabel!
    /* 正文 */
    @IBOutlet weak var contentLabel: KILabel!
    /* 配图的UICollectionView */
    @IBOutlet weak var pictureCollectionView: SWPictureView!
    /* 底部视图 */
    @IBOutlet weak var footView: UIView!
    /* 转发微博内容 */
    @IBOutlet weak var forwardLabel: KILabel!
    
    /* 模型数据 */
    var viewModel: StatusViewModel? {
        didSet {
            /* 1,设置头像 */
            iconImageView.sd_setImageWithURL(viewModel?.iconImage_URL)
            iconImageView.layer.cornerRadius = 30
            /* 2,设置认证头像 */
            verifiedImageView.image = viewModel?.verified_image
            /* 3,设置昵称 */
            nameLabel.text = viewModel?.status.user?.screen_name
            /* 4,设置会员图标 */
            vipImageView.image = nil
            nameLabel.textColor = UIColor.blackColor()
            if let image = viewModel?.vipImage {
                vipImageView.image = image
                nameLabel.textColor = UIColor.orangeColor()
            }
            /* 5,设置时间 */
            timeLabel.text = viewModel?.create_Time
            /* 6,设置来源 */
            sourceLabel.text = viewModel?.source_Text
            /* 7,设置正文 */
            // contentLabel.text = viewModel?.status.text
            contentLabel.attributedText = KeyboardEmoticonPackage.createMutableAttributedStr(viewModel?.status.text ?? "", font: contentLabel.font)
            /* 8,更新配图 */
            pictureCollectionView.viewModel = viewModel
            /* 9,获取转发微博内容 */
            if let tempText = viewModel?.forwardText {
                forwardLabel.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * 10
                // forwardLabel.text = tempText
                forwardLabel.attributedText = KeyboardEmoticonPackage.createMutableAttributedStr(tempText, font: forwardLabel.font)
            }
        }
    }
    
    // MARK : - 外部控制方法
    func calculateRowHeight(viewModel: StatusViewModel) -> CGFloat {
        self.viewModel = viewModel
        self.layoutIfNeeded()
        return CGRectGetMaxY(footView.frame)
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        contentLabel.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * 10
        
        /* 监听@微博账号的点击 */
        contentLabel.userHandleLinkTapHandler = { label, handle, range in
            ZQLog("\(label) \(range)")
        }
        /* 监听话题的点击 */
        contentLabel.hashtagLinkTapHandler = { label, hashtag, range in
            ZQLog("\(label) \(range)")        }
        /* 监听URL的点击 */
        contentLabel.urlLinkTapHandler = { label, url, range in
            ZQLog("\(label) \(range)")
        }

    }   
}

